package com.tech.dcyao.utils;

import com.tech.dcyao.bean.ConfigData;

import static java.lang.Integer.parseInt;

public class Utils {
    public static String setHexArrayToBCC(String dataView) {
        int checksum = 0x00;
        int length = dataView.length();
        for (int i = 0; i < length; i += 2) {
            int int8Num = parseInt(dataView.charAt(i) + "" + dataView.charAt(i+1), 16);
            checksum ^= int8Num;
        }
        // 再将这个数字变成字符串
        String result = Integer.toHexString(checksum);
        if(result.length() == 1) {
            result = "0" + result;
        }
        return result.toUpperCase();
    }
    public static String padStart(String str, int length,String charString) {
        String res = "";
        int len = length - str.length();
        for (int i = 0; i < len; i++) {
            res += charString;
        }
        return res + str;
    }
    // 将数据转换位hex进制
    public static String coverDataToHex (ConfigData jsonData) {
        String str = "557A2E04";
        String isSetTime = jsonData.getIsSetTime();
        if (isSetTime.equalsIgnoreCase("0")) {
            // 如果是手动那么间隔数目就是00
            str += "01";
        } else {
            str += "02";
        }

        String unit = jsonData.getUnit();
        switch (unit) {
            case "s": // 表示为s
                str += "01";
                break;
            case "m": // 表示为分
                str += "02";
                break;
            case "h": // 表示为时
                str += "03";
                break;
        }
        String defineTime = jsonData.getDefineTime();

        String hexUp =padStart(Integer.toHexString(parseInt(defineTime + "")), 2, "0")
                .toUpperCase();
        str += hexUp; // 转换为16进制即可
        // 工作模式 传递的jsonData 然后解析给用户即可
        String workType = jsonData.getWorkType();

        switch (workType) {
            case "P":
                str += "0A";
                break;
            case "AUTO":
                str += "01";
                break;
            case "TV":
                str += "07";
                break;
            case "AV":
                str += "02";
                break;
        }

//        String autoFocus = jsonData.getAutoFocus();
        // 逆向编译即可
        // 需要计算
//        if (autoFocus.equalsIgnoreCase("1")) {
//            str += "01";
//        } else {
//            str += "00";
//        }
        return str;
    }
}
